<?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

$db = new Mypdo();
if(!$_POST){
    if(!empty($_GET['idl'])&&!empty($_GET['idla'])&&!empty($_GET['date'])){
        $loyerManager = new LoyerManager($db);
        $locManager = new LocationManager($db);
        $locaManager = new LocataireManager($db);
        $chargeManager = new ChargeManager($db);
        $loyer = $loyerManager->getLoyerByMoisLocLoca($_GET['date'],$_GET['idl'],$_GET['idla']);
        $loc = $locManager->getLoc($_GET['idl']);
        $pers = $locaManager->getPersonne($_GET['idla']);

        $listeCharge = $chargeManager->getListByIdLoc($_GET['idl']);
        $brutAPayer = $loc->getMontantLoc();
        $netAPayer = $brutAPayer + $brutAPayer*($loc->getTvaLoc()/100);

        foreach ($listeCharge as $value) {
            $montant = $value->getMontantCharge();

            $tva =  ($value->getTvaCharge()/100);
            $charge = ($montant + ($montant * $tva ));
            $netAPayer = $netAPayer+ $charge;
        }
?>

<form action="#" method="post">
    <p>Loyer à payer : <?php echo $netAPayer.'€'; ?></p>
    <label for="montant_loyer" >Montant Payer :</label>
    <input <?php echo $loyer->getSoldePayerLoyer()<=0?'':'value="'.$loyer->getSoldePayerLoyer().'"'; ?>id="montant_loyer" name="montant_loyer" type="number" step="any" min="0" required="required"/><br/>
    <label for="remarque_loyer">Remarque :</label>
    <textarea name="remarque_loyer" id="remarque_loyer"></textarea><br/>
    <input name="net" id="net" type="hidden" value="<?php echo $netAPayer; ?>"/>
    <label for="imp">Imprimer :</label>
    <input type="checkbox" id="imp" name="imp" class="change" checked <?php /* onclick="GereControle('imp','id_pers',1);GereControle('imp','l_pers',1);" */ ?> />
    <label id="l_pers" for="id_pers" class="changer"  >Personne :</label>
    <select class="changer" id="id_pers" name="id_pers" required="" >

        <?php
        if(gettype($pers) == 'object'){
            ?><option selected value="<?php echo $pers->getIdPers(); ?>"><?php echo $pers->getNomPers().' '.$pers->getPrenomPers() ?></option><?php echo "\n";

        }else{
            ?><option></option><?php
            foreach ($pers as $value) {
                //var_dump($value);
                ?>
                <option value="<?php echo $value->getIdPers(); ?>"><?php echo $value->getNomPers().' '.$value->getPrenomPers() ?></option><?php echo "\n"; ?>
            <?php
            }

        }
        ?>
    </select>

    <input type="reset" value="Annuler" /><input type="submit" value="Valider" /></form>

<?php
    }else{
        header('Location: index.php?page=201');
    }
}else{

    $loyerManager = new LoyerManager($db);
    $loyer = $loyerManager->getLoyerByMoisLocLoca($_GET['date'], $_GET['idl'], $_GET['idla']);
    $loyer->setStatutLoyer($_POST['net']<= $_POST['montant_loyer']?1:0);
    $loyer->setRemarqueLoyer($_POST['remarque_loyer']);
    $loyer->setSoldePayerLoyer($_POST['montant_loyer']);

    $loyerManager->update($loyer);


    if($_POST['imp'] != 'on'){
        header('Location: index.php?page=201');
    }else{
        $_SESSION['id_loca'] = $_GET['idla'];
        $_SESSION['id_loc'] = $_GET['idl'];
        $_SESSION['date_loyer'] = $_GET['date'];
        $_SESSION['id_pers'] = $_POST['id_pers'];
        $_SESSION['ligne_max'] = '0';

        header('Location: include/pages/impAppel.php');
    }

}
}else{
    header('Location: index.php');
}
?>